package com.example.ssss;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ssss.data.dao.UserDao;
import com.example.ssss.data.entity.ShareData;
import com.example.ssss.data.entity.User;
import com.example.ssss.ui.loginAndRegister.FillTables;

public class SOSCardActivity extends AppCompatActivity {

    private static final String TAG = "mysql-SSSS-fillTables";
    EditText nameEditText = null;
    EditText heightEditText = null;
    EditText weightEditText = null;
    EditText medicalHistoryEditText = null;
    EditText birthdayEditText = null;
    EditText ABOEditText = null;
    EditText allergicEditText = null;
    EditText habitEditText = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sos_card);
        //隐藏顶部状态栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        nameEditText = findViewById(R.id.nameText);
        heightEditText = findViewById(R.id.heightText);
        weightEditText = findViewById(R.id.weightText);
        medicalHistoryEditText = findViewById(R.id.medicalHistoryText);
        birthdayEditText = findViewById(R.id.birthdayText);
        ABOEditText = findViewById(R.id.ABOtext);
        allergicEditText = findViewById(R.id.allergicText);
        habitEditText = findViewById(R.id.habitText);
        Intent intent = getIntent();
        String fromWhere;
        fromWhere = intent.getStringExtra("data");
        if (fromWhere.equals("fromNotification")){
            new Thread(() -> {
                UserDao userDao = new UserDao();

                ShareData shareData = (ShareData)getApplicationContext();
                User user = null;
                Message msg = null;
                try {
                   user = userDao.findMedical(shareData.userId);
                   msg = new Message();
                   msg.what = 3;
                   msg.obj = user;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                hand.sendMessage(msg);
            }).start();
        }
    }

    public void fillTable(View view){
        String name = nameEditText.getText().toString();
        String height = heightEditText.getText().toString();
        String weight = weightEditText.getText().toString();
        String medicalHistory = medicalHistoryEditText.getText().toString();
        String birthday = birthdayEditText.getText().toString();
        String ABO = ABOEditText.getText().toString();
        String allergic = allergicEditText.getText().toString();
        String habit = habitEditText.getText().toString();

        User user = new User();

        ShareData shareData = (ShareData)getApplicationContext();
        user.setUserId(shareData.userId);
        user.setName(name);
        user.setHigh(height);
        user.setWeight(weight);
        user.setMedicalHistory(medicalHistory);
        user.setBirthday(birthday);
        user.setABO(ABO);
        user.setAllergic(allergic);
        user.setHabits(habit);


        new Thread(() -> {

            int msg = 0;

            UserDao userDao = new UserDao();

            User uu = null;
            try {
                uu = userDao.findUser(user.getUserId());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(uu == null){
                msg = 1;
            }
            else{
                boolean flag = false;
                try {
                    flag = userDao.fillMedical(user);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(flag){
                    msg = 2;
                }
            }
            hand.sendEmptyMessage(msg);
        }).start();
    }
    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler()
    {
        public void handleMessage(Message msg) {
            if(msg.what == 0) {
                Toast.makeText(getApplicationContext(),"填写失败，请重新填写",Toast.LENGTH_LONG).show();
            } else if(msg.what == 1) {
                Toast.makeText(getApplicationContext(),"该账号不存在，请先注册",Toast.LENGTH_LONG).show();
            } else if(msg.what == 2) {
                Toast.makeText(getApplicationContext(), "填写成功", Toast.LENGTH_LONG).show();
                // TODO 跳转到填表
                Intent intent = new Intent();
                intent = new Intent(SOSCardActivity.this, MainActivity.class);
                startActivity(intent);
                //finish();
            }else if(msg.what == 3) {
                User user = (User)msg.obj;
                nameEditText.setText(user.getName());
                heightEditText.setText(user.getHigh());
                weightEditText.setText(user.getWeight());
                medicalHistoryEditText.setText(user.getMedicalHistory());
                birthdayEditText.setText(user.getBirthday());
                ABOEditText.setText(user.getABO());
                allergicEditText.setText(user.getAllergic());
                habitEditText.setText(user.getHabits());
                heightEditText.setText(user.getHigh());
                weightEditText.setText(user.getWeight());
            }
        }
    };
}